package com.dream.system.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_ClassPlantMaster;
import com.dream.model.Sys_LearnPlant;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysUser;

@Controller
@RequestMapping("/edu/sys_ClassPlantAction.do")
@SuppressWarnings("all")
public class Sys_ClassPlantAction extends SuperController {

	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private ISysClass sysClassServiceImpl;
	@Autowired
	private ISysClassPlant sysClassPlantImpl;

	/**
	 * 这里要查询老师所在的学校，查到他们的作业时间
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherClass")
	public void getTeacherClass(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		// SysUser sysUser = (SysUser)
		// request.getSession().getAttribute("sysUser");
		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		// Map map = this.sysUserImpl.getTeacherClass(model);
		Map map = this.sysClassPlantImpl.getTeacherSchool(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_class);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这里要查询老师所在的学校，查到他们的作业时间
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addClassPlantMaster")
	public void addClassPlantMaster(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		// SysUser sysUser = (SysUser)
		// request.getSession().getAttribute("sysUser");
		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			System.out.println("-------------课排表没有老师ID---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		String arr = request.getParameter("arr");
		JSONArray jsonArray = JSONArray.fromObject(arr);

		// Map map = this.sysUserImpl.getTeacherClass(model);
		int map = this.sysClassPlantImpl.addClassPlantMaster(model, jsonArray);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询老师的已经的课排表
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherPlantMaster")
	public void getTeacherPlantMaster(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			System.out.println("-------------课排表没有老师ID---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysClassPlantImpl.getTeacherPlantMaster(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_classPlantMaster);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询老师的已经的课排表
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentPlantMaster")
	public void getStudentPlantMaster(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			System.out.println("-------------课排表没有老师ID---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysClassPlantImpl.getStudentPlantMaster(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_classPlantMaster);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询老师个人的环节表
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherClassPlant")
	public void getTeacherClassPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (sysUser == null || null == sysUser.getTable_id() || "".equals(sysUser.getTable_id())) {
			System.out.println("-------------课排表没有老师ID---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysClassPlantImpl.getTeacherClassPlant(sysUser, model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 更新模板中具体哪一天上什么课。这个课在每周都会被清空一次
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updatePlantMasterValue")
	public void updatePlantMasterValue(Sys_ClassPlantMaster model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String lp = request.getParameter("lp");
		String ml = request.getParameter("ml");
		if (null == lp || "".equals(lp)) {
			System.out.println("-------------没有备课---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if (null == ml || "".equals(ml)) {
			System.out.println("-------------没有课程模板---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		Sys_ClassPlantMaster scm = (Sys_ClassPlantMaster) JSONObject.toBean(JSONObject.fromObject(ml), Sys_ClassPlantMaster.class);
		Sys_LearnPlant slp = (Sys_LearnPlant) JSONObject.toBean(JSONObject.fromObject(lp), Sys_LearnPlant.class);
		scm.setSys_LearnPlant(slp); 
		Sys_ClassPlantMaster map = this.sysClassPlantImpl.updatePlantMasterValue(scm);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_classPlantMaster);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 这个课在每周都会被清空一次，这里把他手动全部清空
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateMasterWeekValue")
	public void updateMasterWeekValue(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		
		if(null == model || null ==model.getTable_id() ||"".equals(model.getTable_id())){
			Msg msg = this.getMsg("0", "没有用户可以操作", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		
		int map = this.sysClassPlantImpl.updateMasterWeekValue(model);
		if(map==0){
			Msg msg = this.getMsg("0", "用户名密码错误", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}else{
			Msg msg = this.getMsg("1", "操作成功", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

	}
	
	
}
